import useUserStore from '@/stores/user.js'
import request from '@/utils/request.js'

const userStore = useUserStore()


function setStreamHeader() {
  return {
    "Content-Type": "application/json",
    "token": userStore.getToken
  }
}

export function streamRequest(data, url, method = 'POST') {
  const host = import.meta.env.VITE_BASE_API
  const control = new AbortController()
  const signal = control.signal
  return new Promise((resolve, reject) => {
    const fetchOptions = {
      method: method,
      headers: setStreamHeader(),
      mode: "cors",
      cache: "default",
      body: JSON.stringify(data),
      signal: signal
    }
    try {
      fetch(host + url, fetchOptions).then(response => {
        if (!response.ok) {
          reject(new Error("Network response error:" + response.statusText))
        }
        const {body, headers} = response
        resolve({
          reader: body.getReader(),
          header: headers,
          control: control
        })
      }).catch(error => {
        reject(error)
      })
    } catch (err) {
      reject(err)
    }
  })
}

export function defaultChat(data) {
  return streamRequest(data, '/chat/completions')
}

export function knowledgeChat(data) {
  return streamRequest(data, '/chat/knowledge')
}

export function fileChat(data) {
  return streamRequest(data, '/chat/file_chat')
}

export function courseChat(data) {
  return streamRequest(data, '/study/stream-chat')
}

export function httpQuestions(data) {
  return request({
    url: '/chat/questions',
    method: 'post',
    data
  })
}

export function httpGetHistories() {
  return request({
    url: '/chat/histories',
    method: 'get'
  })
}

export function httpGetSession(session_id) {
  const params = {session_id}
  return request({
    url: '/chat/session',
    method: 'get',
    params
  })
}

export function httpDeleteSession(session_id) {
  const params = {session_id}
  return request({
    url: '/chat/session-del',
    method: 'get',
    params
  })
}

export function httpLikeHandle(id, type, content, question_id = []) {
  const data = {chat_history_id: id, like_type: type, content, question_id}
  return request({
    url: '/chat/like',
    method: 'post',
    data
  })
}

/**
 *
 * @param {int} chat_history_id
 * @returns {Promise<AxiosResponse<Any>>}
 */
export const addNoteMessage = (chat_history_id) => {
  const data = {chat_history_id}
  return request({
    url: '/note/add',
    method: 'post',
    data
  })
}

/**
 *
 * @param {int} note_id
 * @returns {Promise<AxiosResponse<Any>>}
 */
export const delNoteMessage = (note_id) => {
  const data = {note_id}
  return request({
    url: '/note/del',
    method: 'post',
    data
  })
}

export const getNoteList = (params) => {
  return request({
    url: '/note/details',
    method: 'get',
    data
  })
}

export const httpGetLastId = (conversation_id) => {
  const params = {conversation_id}
  return request({
    url: '/chat/last-id',
    method: 'get',
    params
  })
}

export const getLikeQuestions = () => {
  return request({
    url: '/chat/like/questions',
    method: 'get'
  })
}
/**
 * 获取模型列表
 * @returns 
 */
export const httpModels = () => {
  return request({
    url: '/chat/models',
    method: 'get'
  })
}
